package com.ece.demo.init;

import java.io.File;

import org.apache.commons.lang3.StringUtils;

import dev.ece.core.texture.Texture;
import dev.ece.suit.loader.TextureFactory;
import dev.ece.suit.loader.TextureLoader;
import dev.ece.util.tools.Constants;
import dev.ece.util.tools.ploy.TextureEdgePloy;

public class Model3DTextureLoader implements TextureLoader {

	static {
		TextureFactory.setTextureLoader(new Model3DTextureLoader());
	}
	
	@Override
	public Texture createTexture(String name) {
		String filename = new File(name).getName();
		if(StringUtils.equalsIgnoreCase("dds", getExt(filename))) {
			filename = StringUtils.join(filename, ".jpg");
		}
		File file = Constants.getGameFile(StringUtils.join("/assets/models/", filename));
		if(file.exists()) {
			return Texture.newInstance(file, TextureEdgePloy.getInstance());
		}
		return null;
	}

	public static String getExt(String name) {
		String[] array = StringUtils.split(name, ".");
		return StringUtils.lowerCase(array[array.length - 1]);
	}
	
}
